<template>
	<view class="promote">
		<view v-if="promoteList.length">
			<view class="promote-item" v-for="(data,index) in promoteList" :key="index">
				<view class="promote-item-hd">
					<image :src="data.img" mode="aspectFill"></image>
					<view class="promote-item-info">
						<view class="promote-item-tit">{{data.title}}</view>
						<view class="promote-item-txt">{{data.message}}</view>
					</view>
				</view>
				<view class="promote-item-bd">
					<button plain type="primary" @click="generateCode" hover-class="">生成二维码</button>
					<button type="primary" @click="poster(data.one_img)" hover-class="">生成海报</button>
				</view>
			</view>
		</view>
		<nothing :text="'暂无推广内容'" :btn="false" v-else></nothing>
		
		<uni-popup ref="code">
			<view class="generateCode-pop">
				<image src="https://master.shengejiaju.cn/web/static/icons/ico_closed002.png" class="close" @click="close"></image>
				<view class="generateCode-pop-box">
					<view class="generateCode-pop-tit">生成二维码成功!</view>
					<view class="generateCode-pop-info">扫码注册即可完成邀请</view>
					<canvas canvas-id="code" :style="'width:'+width+'px; height:'+height+'px; margin: 40rpx auto;'"></canvas>
					<button type="primary" color="#2073F5" @click="save">保存到手机</button>
				</view>
			</view>
		</uni-popup>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page: 0,
				noMore: false,
				QrSrc: '',
				promoteList: [],
				animationData: {},
				maskAnimateionData: {},
				width: 0,
				height: 0
			};
		},
		onLoad() {
			this.promoteList = []
			this.init()
			this.getQRcode()
		},
		onShow() {
			
		},
		onReachBottom() {
			if(!this.noMore){
				this.page++
				this.init()
			}
		},
		methods: {
			getQRcode(){
				this.$api.GetQRcode().then(qrRes => {
					this.QrSrc = qrRes.datas
				}).then(qrRes => {
					this.width = uni.upx2px(350)
					this.height = uni.upx2px(350)
				})
			},
			init(){
				this.$api.ActivityList().then(res => {
					if(res.datas.list.length){
						res.datas.list.forEach(item => {
							this.promoteList.push(item)
						})
					}else{
						this.noMore = true
					}
				})
			},
			generateCode(){
				this.OnCanvas()
				this.$refs.code.open()
			},
			close(){
				this.$refs.code.close()
			},
			async OnCanvas() {
				let _this = this;
				uni.showLoading({
					title: '正在生成二维码',
					mask: true
				})
				_this.ctx = uni.createCanvasContext('code');
				const _QrCode = await _this.getImageInfo({ imgSrc: _this.QrSrc }); //二维码
				
				_this.ctx.setFillStyle('#FFFFFF');
				_this.ctx.fillRect(0,0,uni.upx2px(350),uni.upx2px(350))
				
				//添加二维码
				_this.ctx.drawImage(_QrCode.path, 0, 0, uni.upx2px(350),uni.upx2px(350));
				//添加二维码 end
				
				//延迟后渲染至canvas上
				setTimeout(function() {
					_this.ctx.draw();
				}, 600);
			},
			async getImageInfo({ imgSrc }) {
				return new Promise((resolve, errs) => {
					uni.getImageInfo({
						src: imgSrc,
						success: function(image) {
							uni.hideLoading()
							resolve(image);
						},
						fail(err) {
							uni.hideLoading()
							uni.showToast({
								title: '生成失败，请退出重试',
								icon: 'none',
								duration: 1500
							})
							errs(err);
						}
					});
				});
			},
			poster(img){
				uni.navigateTo({
					url: '/pages/my/promote/poster?img='+img
				})
			},
			save(){
				uni.showLoading({
					title: '正在保存',
					mask: true
				})
				uni.canvasToTempFilePath({
				  destWidth: uni.upx2px(350),
				  destHeight: uni.upx2px(350),
				  canvasId: 'code',
				  success: function(data) {
						uni.hideLoading()
						uni.saveImageToPhotosAlbum({
							filePath: data.tempFilePath,
							success(saveRes) {
								uni.showToast({
									icon: 'none',
									title: '保存成功，请在相册中查看',
									duration: 1500
								})
							},
							fail(fail) {
								uni.hideLoading()
							}
						})
				  }
				})
			}
		},
		mounted() {
			let _this = this;
		}
	}
</script>

<style lang="scss">
	.promote{
		padding-top: 20rpx;
	}
	.promote-item{
		margin-bottom: 20rpx;
		background-color: #FFF;
		padding: 40rpx 20rpx;
		.promote-item-hd{
			display: flex;
			align-items: flex-start;
			image{
				width: 130rpx;
				height: 130rpx;
				vertical-align: top;
				margin-right: 30rpx;
				background-color: #EEE;
			}
			.promote-item-info{
				flex: 1;
				min-width: 0;
				.promote-item-tit{
					font-size: 34rpx;
					margin: 0 0 20rpx;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				.promote-item-txt{
					font-size: 28rpx;
					color: #999;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}	
		}
		.promote-item-bd{
			display: flex;
			justify-content: flex-end;
			border-top: solid 1px #EEE;
			padding-top: 40rpx;
			margin-top: 40rpx;
			button{
				display: inline-block;
				margin: 0;
				height: 60rpx;
				line-height: 60rpx;
				font-size: 28rpx;
				margin-left: 30rpx;
			}
		}
	}
	.generateCode-pop{
		text-align: right;
		image.close{
			width: 68rpx;
			height: 68rpx;
			vertical-align: top;
			margin-bottom: 20rpx;
		}
		.generateCode-pop-box{
			background-color: #FFF;
			width: 560rpx;
			height: 680rpx;
			border-radius: 20rpx;
			text-align: center;
			.generateCode-pop-tit{
				font-size: 38rpx;
				padding: 30rpx 0 20rpx;
			}
			.generateCode-pop-info{
				color: #999;
				font-size: 26rpx;
			}
			button{
				border-radius: 100rpx;
				width: 350rpx;
				height: 78rpx;
				line-height: 78rpx;
				font-size: 30rpx;
			}
		}
	}
</style>
